def number(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True


x = int(input())
if x >= 1000 and x <= 9999 and x==int(x):
    max = 0
    if number(x):
        max = x
    else:
        y = int(x % 10)
        z = int(x % 100 // 10)
        w = int(x // 100 % 10)
        u = int(x // 1000)

        if number(u * 100 + w * 10 + z) > max:
            max = int(u * 100 + w * 10 + z)
        if number(w * 100 + z * 10 + y) > max:
            max = int(w * 100 + z * 10 + y)
        if number(u * 10 + w) > max:
            max = int(u * 10 + w)
        if number(w * 10 + z) > max:
            max = int(w * 10 + z)
        if number(z * 10 + y) > max:
            max = int(z * 10 + y)
        if number(u) > max:
            max = int(u)
        if number(w) > max:
            max = int(w)
        if number(z) > max:
            max = int(z)
        if number(y) > max:
            max = int(y)
    print(max)
else:
    print(0)